##############################################################
#Replication file for:
#The Importance of a Liberal Power's Attention to Democratic Elections Around the World
#Johannes Bubeck Ashrakat Elshehawy Nikolay Marinov Federico Nanni
##############################################################


rm(list = ls())

#needed libraries
library(readr)
library(ggplot2)

#set working directory
setwd("/Data")
#read data
df1 <- read_delim("resultsfigured8_1.csv",
                   ",",
                   escape_double = FALSE, trim_ws = TRUE)
df2 <- read_delim("resultsfigured8_2.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df3 <- read_delim("resultsfigured8_3.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df4 <- read_delim("resultsfigured8_4.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df5 <- read_delim("resultsfigured8_5.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df6 <- read_delim("resultsfigured8_6.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df7 <- read_delim("resultsfigured8_7.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df8 <- read_delim("resultsfigured8_8.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df9 <- read_delim("resultsfigured8_9.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df10 <- read_delim("resultsfigured8_10.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df11 <- read_delim("resultsfigured8_11.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df12 <- read_delim("resultsfigured8_12.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df13 <- read_delim("resultsfigured8_13.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df14 <- read_delim("resultsfigured8_14.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df15 <- read_delim("resultsfigured8_15.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df16 <- read_delim("resultsfigured8_16.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df17 <- read_delim("resultsfigured8_17.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)
df18 <- read_delim("resultsfigured8_18.csv",
                  ",",
                  escape_double = FALSE, trim_ws = TRUE)

dfall=rbind(df1,df2,df3,df4,df5,df6,df7,df8,df9,
      df10,df11,df12,df13,df14,df15,df16,df17,df18)

level_order <- c("1",
                 "2",
                 "3",
                 "4",
                 "5",
                 "6",
                 "7",
                 "8",
                 "9",
                 "10",
                 "11",
                 "12",
                 "13",
                 "14",
                 "15",
                 "16",
                 "17",
                 "18"
                  ) #this vector might be useful for other plots/analyses


ggplot(dfall, aes(x = factor(months, level=level_order),
                                  y = mean_difference)) + theme_bw()+
  geom_point(size = 2.5) +
  geom_errorbar(aes(ymax = lower_confidence, ymin = upper_confidence,
                    width = 0))+
  theme(
        axis.text=element_text(size=12),
        axis.title.y = element_text(size=13),
        axis.title.x = element_text(size=13)
        ) +
  xlab("# of months away from a US Presidential Elections")+
  ylab("Predicted change")

